package com.mjf.utils;

import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.dic.Dictionary;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Desc: IK 分词器工具类
 */
public class KeywordUtil {

    /**
     * 使用 IK 分词器对字符串进行分词
     * @param text 要分词的字符串
     * @return 分词后的结果
     */
    public static List<String> analyze(String text) {
        StringReader sr = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(sr, true);
        Lexeme lex = null;
        List<String> keywordList = new ArrayList();

        while (true) {
            try {
                if ((lex = ik.next()) != null) {
                    String lexemeText = lex.getLexemeText();    // 词元的文本内容
                    keywordList.add(lexemeText);
                } else {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return keywordList;
    }

    /**
     * 扩展词条后，使用 IK 分词器对字符串进行分词
     * @param text 要分词的字符串
     * @return 分词后的结果
     */
    public static List<String> analyzeExt(String text) {
        StringReader sr = new StringReader(text);
        Configuration cfg = getConfiguration();
        IKSegmenter ik = new IKSegmenter(sr, cfg);
        Lexeme lex = null;
        List<String> keywordList = new ArrayList();

        while (true) {
            try {
                if ((lex = ik.next()) != null) {
                    String lexemeText = lex.getLexemeText();    // 词元的文本内容
                    keywordList.add(lexemeText);
                } else {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return keywordList;
    }

    private static Configuration getConfiguration() {
        // 获取默认配置文件
        Configuration cfg = DefaultConfig.getInstance();
        cfg.setUseSmart(true);

        // 词典初始化
        Dictionary.initial(cfg);
        Dictionary dictionary = Dictionary.getSingleton();

        // 获取扩展词典路径，使用 IKAnalyzer.cfg.xml 进行配置
        String extDictionary = cfg.getExtDictionarys().get(0);

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(Objects.requireNonNull(KeywordUtil.class.getResource("/ext.dic")).getPath()));
//            br = new BufferedReader(new FileReader(extDictionary));
            String word;
            ArrayList<String> words = new ArrayList<>();

            while ((word = br.readLine()) != null) {
                words.add(word);
            }

            // 批量加载新词条
            dictionary.addWords(words);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取异常");
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件关闭异常");
                }
            }
        }

        return cfg;
    }

    public static void main(String[] args) {
        System.out.println(KeywordUtil.analyze("Apple iPhoneXSMax (A2104) 256GB 深空灰色 移动联通电信 4G 手机 双卡双待"));

        System.out.println(KeywordUtil.analyze("蓝瘦香菇"));
        System.out.println(KeywordUtil.analyze("地球永远的神"));

        System.out.println("===============================");

        System.out.println(KeywordUtil.analyzeExt("蓝瘦香菇"));
        System.out.println(KeywordUtil.analyzeExt("地球永远的神"));
    }
}